import type { IntlConfig } from 'react-intl';
import { createIntl, createIntlCache } from 'react-intl';
import * as en_US from './en-US';
import { getLocale } from './intlUtil';
import * as zh_CN from './zh-CN';

function loadLocale(lang: string) {
  let locale = null;
  let message: any = null;

  switch (lang) {
    case 'en-US':
      locale = 'en-US';
      message = en_US;
      break;
    default:
      locale = 'zh-CN';
      message = zh_CN;
      break;
  }

  return { locale, messages: message } as IntlConfig;
}

const cache = createIntlCache();

const locale = getLocale();
const localeConfig: IntlConfig = loadLocale(locale);

const intl = createIntl(localeConfig, cache);

export const i18n = intl.formatMessage;

export default intl;
